//+------------------------------------------------------------------+
//|                                         TrendAnalysisNew.mqh |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024"
#property link      ""
#property version   "1.00"
#property strict

// 趋势类型枚举
enum ENUM_TREND_TYPE {
    TREND_UP,        // 上升趋势
    TREND_DOWN,      // 下降趋势
    TREND_SIDEWAYS   // 盘整趋势
};

// 趋势结构
struct Trend {
    ENUM_TREND_TYPE type;     // 趋势类型
    int startBar;             // 起始K线
    int endBar;               // 结束K线
    double startPrice;        // 起始价格
    double endPrice;          // 结束价格
    double strength;          // 趋势强度
    bool isActive;            // 是否活跃
};

// 趋势分析类
class CTrendAnalysis {
private:
    Trend m_trends[];         // 趋势数组
    int m_trendCount;         // 趋势计数
    int m_currentTrend;       // 当前趋势索引
    
    // 趋势识别参数
    int m_maPeriod;           // 移动平均线周期
    int m_adxPeriod;          // ADX周期
    double m_adxThreshold;    // ADX阈值
    double m_trendThreshold;  // 趋势阈值
    
    // 指标句柄
    int m_maHandle;           // 移动平均线句柄
    int m_adxHandle;          // ADX句柄
    
public:
    // 构造函数
    CTrendAnalysis() {
        m_trendCount = 0;
        m_currentTrend = -1;
        m_maPeriod = 20;          // 默认MA周期
        m_adxPeriod = 14;         // 默认ADX周期
        m_adxThreshold = 25.0;    // 默认ADX阈值
        m_trendThreshold = 0.5;   // 默认趋势阈值
    }
    
    // 初始化
    bool Initialize(int maPeriod = 20, int adxPeriod = 14, double adxThreshold = 25.0, double trendThreshold = 0.5) {
        m_maPeriod = maPeriod;
        m_adxPeriod = adxPeriod;
        m_adxThreshold = adxThreshold;
        m_trendThreshold = trendThreshold;
        
        // 创建指标句柄
        m_maHandle = iMA(Symbol(), PERIOD_CURRENT, m_maPeriod, 0, MODE_SMA, PRICE_CLOSE);
        if(m_maHandle == INVALID_HANDLE) return false;
        
        m_adxHandle = iADX(Symbol(), PERIOD_CURRENT, m_adxPeriod);
        if(m_adxHandle == INVALID_HANDLE) return false;
        
        return true;
    }
    
    // 分析价格数据
    bool AnalyzePriceData(const double &high[], const double &low[], const double &close[], int count) {
        if(count < m_maPeriod) return false;
        
        // 重置趋势数组
        ArrayResize(m_trends, 0);
        m_trendCount = 0;
        m_currentTrend = -1;
        
        // 获取指标数据
        double maBuffer[];
        double adxBuffer[];
        double plusDI[];
        double minusDI[];
        
        ArraySetAsSeries(maBuffer, true);
        ArraySetAsSeries(adxBuffer, true);
        ArraySetAsSeries(plusDI, true);
        ArraySetAsSeries(minusDI, true);
        
        if(CopyBuffer(m_maHandle, 0, 0, count, maBuffer) != count) return false;
        if(CopyBuffer(m_adxHandle, 0, 0, count, adxBuffer) != count) return false;
        if(CopyBuffer(m_adxHandle, 1, 0, count, plusDI) != count) return false;
        if(CopyBuffer(m_adxHandle, 2, 0, count, minusDI) != count) return false;
        
        // 分析趋势
        for(int idx = m_maPeriod; idx < count; idx++) {
            if(IsTrendChange(maBuffer, adxBuffer, plusDI, minusDI, idx)) {
                AddTrend(high, low, close, maBuffer, adxBuffer, plusDI, minusDI, idx);
            }
        }
        
        return m_trendCount > 0;
    }
    
    // 检查趋势是否改变
    bool IsTrendChange(const double &maBuffer[], const double &adxBuffer[], 
                      const double &plusDI[], const double &minusDI[], int barIndex) {
        // 检查ADX强度
        if(adxBuffer[barIndex] < m_adxThreshold) return false;
        
        // 检查趋势方向
        bool isUpTrend = plusDI[barIndex] > minusDI[barIndex];
        bool wasUpTrend = plusDI[barIndex + 1] > minusDI[barIndex + 1];
        
        return isUpTrend != wasUpTrend;
    }
    
    // 添加新趋势
    void AddTrend(const double &high[], const double &low[], const double &close[],
                 const double &maBuffer[], const double &adxBuffer[],
                 const double &plusDI[], const double &minusDI[], int barIndex) {
        Trend trend;
        trend.startBar = barIndex;
        trend.startPrice = close[barIndex];
        trend.isActive = true;
        
        // 确定趋势类型
        if(plusDI[barIndex] > minusDI[barIndex]) {
            trend.type = TREND_UP;
        } else if(plusDI[barIndex] < minusDI[barIndex]) {
            trend.type = TREND_DOWN;
        } else {
            trend.type = TREND_SIDEWAYS;
        }
        
        // 计算趋势强度
        trend.strength = adxBuffer[barIndex] / 100.0;
        
        // 添加到趋势数组
        ArrayResize(m_trends, m_trendCount + 1);
        m_trends[m_trendCount++] = trend;
        m_currentTrend = m_trendCount - 1;
    }
    
    // 更新当前趋势
    bool UpdateCurrentTrend(const double &high[], const double &low[], const double &close[],
                          const double &maBuffer[], const double &adxBuffer[],
                          const double &plusDI[], const double &minusDI[], int barIndex) {
        if(m_currentTrend < 0) return false;
        
        Trend &currentTrend = m_trends[m_currentTrend];
        
        // 更新趋势结束位置
        currentTrend.endBar = barIndex;
        currentTrend.endPrice = close[barIndex];
        
        // 检查趋势是否结束
        if(IsTrendChange(maBuffer, adxBuffer, plusDI, minusDI, barIndex)) {
            currentTrend.isActive = false;
            return true;
        }
        
        return false;
    }
    
    // 获取当前趋势
    bool GetCurrentTrend(Trend &trend) {
        if(m_currentTrend < 0 || m_currentTrend >= m_trendCount) return false;
        
        trend = m_trends[m_currentTrend];
        return true;
    }
    
    // 获取趋势数量
    int GetTrendCount() {
        return m_trendCount;
    }
    
    // 获取指定索引的趋势
    bool GetTrend(int index, Trend &trend) {
        if(index < 0 || index >= m_trendCount) return false;
        
        trend = m_trends[index];
        return true;
    }
    
    // 预测趋势延续性
    bool PredictTrendContinuation(Trend &predictedTrend) {
        if(m_currentTrend < 0) return false;
        
        Trend currentTrend = m_trends[m_currentTrend];
        
        // 预测趋势延续
        predictedTrend = currentTrend;
        predictedTrend.startBar = currentTrend.endBar;
        predictedTrend.startPrice = currentTrend.endPrice;
        
        // 根据当前趋势强度预测
        predictedTrend.strength = currentTrend.strength * 0.8; // 假设趋势强度会略微减弱
        
        predictedTrend.isActive = true;
        return true;
    }
    
    // 验证趋势强度
    bool ValidateTrendStrength() {
        if(m_currentTrend < 0) return false;
        
        Trend currentTrend = m_trends[m_currentTrend];
        
        // 检查趋势强度是否足够
        return currentTrend.strength >= m_trendThreshold;
    }
    
    // 清理过期趋势
    void CleanupOldTrends(int maxAge) {
        for(int idx = m_trendCount - 1; idx >= 0; idx--) {
            if(!m_trends[idx].isActive && 
               (m_trends[idx].endBar - m_trends[idx].startBar) > maxAge) {
                // 移除过期趋势
                for(int jdx = idx; jdx < m_trendCount - 1; jdx++) {
                    m_trends[jdx] = m_trends[jdx + 1];
                }
                m_trendCount--;
            }
        }
    }
    
private:
    // 获取MA值
    double GetMAValue(int shift) {
        double maBuffer[];
        ArraySetAsSeries(maBuffer, true);
        
        bool maCopySuccess = (CopyBuffer(m_maHandle, 0, shift, 1, maBuffer) == 1);
        if(!maCopySuccess) {
            return 0.0;
        }
        
        return maBuffer[0];
    }
    
    // 获取ADX值
    double GetADXValue(int shift) {
        double adxBuffer[];
        ArraySetAsSeries(adxBuffer, true);
        
        bool adxCopySuccess = (CopyBuffer(m_adxHandle, 0, shift, 1, adxBuffer) == 1);
        if(!adxCopySuccess) {
            return 0.0;
        }
        
        return adxBuffer[0];
    }
}; 